package com.xh.bussiness.teachbook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.teachbook.bean.dto.PreTeachingBookPracticeStudentResultDto;
import com.xh.bussiness.teachbook.bean.dto.TeachingBookPracticeContentDto;
import com.xh.bussiness.teachbook.bean.dto.TeachingBookPracticeDto;
import com.xh.bussiness.teachbook.bean.dto.TeachingBookQuestionStudentResultDto;
import com.xh.bussiness.teachbook.constant.TeachingBookConstant;
import com.xh.bussiness.teachbook.dao.TeachingBookPracticeContentMapper;
import com.xh.bussiness.teachbook.dao.TeachingBookPracticeMapper;
import com.xh.bussiness.teachbook.model.TeachingBook;
import com.xh.bussiness.teachbook.model.TeachingBookPractice;
import com.xh.bussiness.teachbook.model.TeachingBookPracticeContent;
import com.xh.bussiness.teachbook.model.TeachingBookStudentQuestionResult;
import com.xh.bussiness.teachbook.service.ITeachingBookNumberService;
import com.xh.bussiness.teachbook.service.ITeachingBookPracticeService;
import com.xh.bussiness.teachbook.service.ITeachingBookService;
import com.xh.bussiness.teachbook.service.ITeachingBookStudentQuestionResultService;
import com.xh.bussiness.tiku.bean.dto.QuestionDto;
import com.xh.bussiness.tiku.service.IQuestionService;
import com.xh.core.service.impl.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


@Service
public class TeachingBookPracticeServiceImpl extends BaseServiceImpl<TeachingBookPracticeMapper, TeachingBookPractice> implements ITeachingBookPracticeService {
    public static final Logger log = LoggerFactory.getLogger(TeachingBookPracticeServiceImpl.class);
    @Autowired
    private ITeachingBookService teachingBookService;
    @Autowired
    private TeachingBookPracticeContentMapper teachingBookPracticeContentMapper;
    @Autowired
    private IQuestionService questionService;
    @Autowired
    private TeachingBookPracticeMapper teachingBookPracticeMapper;
    @Autowired
    private ITeachingBookNumberService teachingBookNumberService;
    @Autowired
    private ITeachingBookStudentQuestionResultService teachingBookStudentQuestionResultService;

    @Override
    public TeachingBookPracticeDto getTeachingBookPracticeByTeachingBookId(String teachingBookId) {
        QueryWrapper<TeachingBookPractice> qw = new QueryWrapper<>();
        qw.eq("teaching_book_id", teachingBookId);
        TeachingBookPractice teachingBookPractice = this.selectOne(qw, TeachingBookPractice.class);

        List<TeachingBookPracticeContentDto> practiceContentDtos = new ArrayList<>();
        TeachingBookPracticeDto practiceDto = new TeachingBookPracticeDto();
        if (teachingBookPractice == null) {
            practiceDto.setContentDtos(new ArrayList<>());
            return practiceDto;
        }

        QueryWrapper<TeachingBookPracticeContent> contentQueryWrapper = new QueryWrapper<>();
        contentQueryWrapper.eq("practice_id", teachingBookPractice.getId());
        contentQueryWrapper.orderByAsc("order_no");
        List<TeachingBookPracticeContent> contents = teachingBookPracticeContentMapper.selectList(contentQueryWrapper);
        if (CollectionUtils.isEmpty(contents)) {
            practiceDto.setContentDtos(new ArrayList<>());
            return practiceDto;
        }

        for (TeachingBookPracticeContent content : contents) {
            TeachingBookPracticeContentDto contentDto = new TeachingBookPracticeContentDto();
            contentDto.setId(content.getId());
            contentDto.setBlankLine(content.getBlankLine());
            contentDto.setContentType(content.getContentType());
            contentDto.setContent(content.getContent());
            contentDto.setScore(content.getScore());
            contentDto.setOrderNo(content.getOrderNo());

            if (content.getContentType().equals(TeachingBookConstant.PracticeContentType.QUESTION)) {
                QuestionDto question = questionService.getQuestion(content.getQuestionId(), null);
                if (question == null) {
                    log.error("练习试题不存在" + content.getQuestionId());
                    continue;
                }
                contentDto.setQuestion(question);
                contentDto.setQuestionId(content.getQuestionId());
                contentDto.setSubject(content.getSubject());
            }
            practiceContentDtos.add(contentDto);
        }
        practiceDto.setId(teachingBookPractice.getId());
        practiceDto.setTeachingBookId(teachingBookPractice.getTeachingBookId());
        practiceDto.setTotalScore(teachingBookPractice.getTotalScore());
        practiceDto.setUseTime(teachingBookPractice.getUseTime());
        practiceDto.setContentDtos(practiceContentDtos);
        practiceDto.setYear(teachingBookPractice.getYear());
        practiceDto.setMonth(teachingBookPractice.getMonth());
        practiceDto.setDay(teachingBookPractice.getDay());

        return practiceDto;
    }

    /**
     * 获取上一节备课练习详情
     */
    @Override
    public PreTeachingBookPracticeStudentResultDto queryPreTeachingBookPracticeDetail(String lwStudentId, String teachingBookId) {
        TeachingBook teachingBook = teachingBookService.getById(teachingBookId);
        if (teachingBook == null) {
            return new PreTeachingBookPracticeStudentResultDto();
        }
        QueryWrapper<TeachingBookPractice> practiceQueryWrapper = new QueryWrapper<>();
        practiceQueryWrapper.eq("teaching_book_id", teachingBookId);
        TeachingBookPractice teachingBookPractice = teachingBookPracticeMapper.selectOne(practiceQueryWrapper);
        if (teachingBookPractice == null) {
            return new PreTeachingBookPracticeStudentResultDto();
        }

        QueryWrapper<TeachingBookPracticeContent> contentQueryWrapper = new QueryWrapper<>();
        contentQueryWrapper.eq("teaching_book_id", teachingBook.getId());
        //1.试题;2.文本
        contentQueryWrapper.eq("content_type", 1);
        contentQueryWrapper.orderByAsc("order_no");
        List<TeachingBookPracticeContent> teachingBookPracticeContents = teachingBookPracticeContentMapper.selectList(contentQueryWrapper);
        if (CollectionUtils.isEmpty(teachingBookPracticeContents)) {
            return new PreTeachingBookPracticeStudentResultDto();
        }

        PreTeachingBookPracticeStudentResultDto dto = new PreTeachingBookPracticeStudentResultDto();
        dto.setTopic(teachingBook.getTopic());
        dto.setQuestCount(teachingBookPracticeContents.size());
        dto.setTotalScore(teachingBookPractice.getTotalScore());
        List<TeachingBookQuestionStudentResultDto> questionStudentResultDtos = new ArrayList<>();
        for (TeachingBookPracticeContent teachingBookPracticeContent : teachingBookPracticeContents) {
            TeachingBookQuestionStudentResultDto studentResultDto = new TeachingBookQuestionStudentResultDto();

            QuestionDto question = questionService.getQuestion(teachingBookPracticeContent.getQuestionId(), null);
            if (question == null) {
                log.error("当前试题不存在" + teachingBookPracticeContent.getQuestionId());
                continue;
            }
            studentResultDto.setQuestionDto(question);
            studentResultDto.setQuestionScore(teachingBookPracticeContent.getScore());
            //查询错题作答结果
            TeachingBookStudentQuestionResult questionResult = teachingBookStudentQuestionResultService.queryOneByParam(lwStudentId, teachingBookId, teachingBookPracticeContent.getQuestionId(),3);
            if (questionResult != null) {
                studentResultDto.setCorrect(questionResult.getCorrect());
            }
            questionStudentResultDtos.add(studentResultDto);
        }
        dto.setPracticeQuestion(questionStudentResultDtos);
        dto.setTeachingBookPracticeId(teachingBookPractice.getId());
        return dto;
    }
}
